//
//  CZTabBar.swift
//  weibo
//
//  Created by 丁蒙 on 16/6/25.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit

class CZTabBar: UITabBar {
    
    var colsure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        //设置撰写按钮的frame
        composeButton.center.x = self.bounds.size.width * 0.5
        composeButton.center.y = self.bounds.size.height * 0.5
        
        let buttonWidth = self.bounds.size.width / 5
        var index: CGFloat = 0
        
        for value in self.subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.frame.size.width = buttonWidth
                value.frame.origin.x = index * buttonWidth
                
                index++
                
                if index == 2 {
                    index++
                }
            }
        }
        
    }
    
    //撰写按钮
    private lazy var composeButton: UIButton = {
       let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        button.addTarget(self, action: #selector(CZTabBar.composeButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    // MARK - 撰写按钮监听事件
    @objc private func composeButtonClick() {
        
        colsure?()
    }

}
